package com.qtenv.datasync.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.turf.TurfJoins;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName Test1
 * @Author CBL
 * @Date 2022/7/13 11:08
 **/
public class Test1 {
    public static void main(String[] args) {
        List<String[]> resultList = CollectionUtil.newArrayList();

        JSONObject jsonObject = JSONUtil.parseObj(new FileReader("440118.geojson").readString());
        JSONArray jsonArray = jsonObject.getJSONArray("features");

        List<Object> points = jsonArray.stream().filter(item -> {
            JSONObject j = JSONUtil.parseObj(item);
            return j.getJSONObject("geometry").getStr("type").equals("Point");
        }).collect(Collectors.toList());

        List<Object> lines = jsonArray.stream().filter(item -> {
            JSONObject j = JSONUtil.parseObj(item);
            return j.getJSONObject("geometry").getStr("type").equals("LineString");
        }).collect(Collectors.toList());

        lines.forEach(line -> {
            JSONArray coors = JSONUtil.parseObj(line).getJSONObject("geometry").getJSONArray("coordinates");
            coors.remove(coors.size() - 1);
            coors.add(coors.get(0));

            List<Point> linePoints = coors.stream().map(item -> {
                String[] dd = item.toString().replace("[", "").replace("]", "").split(",");
                return Point.fromLngLat(Double.valueOf(dd[0]), Double.valueOf(dd[1]));
            }).collect(Collectors.toList());
            String coordinates = coors.join(",");

            List<Object> pointsInPolygon = points.stream().filter(p -> {
                JSONArray pp = JSONUtil.parseObj(p).getJSONObject("geometry").getJSONArray("coordinates");
                String[] dd = pp.toString().replace("[", "").replace("]", "").split(",");
                Point pt = Point.fromLngLat(Double.valueOf(dd[0]), Double.valueOf(dd[1]));
                boolean a = TurfJoins.inside(pt, Polygon.fromLngLats(Arrays.asList(linePoints)));
//                if (a) {
//                    System.out.println(pp + " 在" + JSONUtil.parseObj(line).getJSONObject("properties").getStr("id") + "平面内");
//                }
                return a;
            }).collect(Collectors.toList());

            if (pointsInPolygon.size() == 1) {
                String text = JSONUtil.parseObj(pointsInPolygon.get(0)).getJSONObject("properties").getJSONObject("style").getStr("text");
//                if (text.contains("片区")) {
//                    System.out.println("Polygon " + JSONUtil.parseObj(line).getJSONObject("properties").getStr("id") + " has " + pointsInPolygon.size() + " points inside");
//                    System.out.println(JSONUtil.toJsonPrettyStr(pointsInPolygon.get(0)));
//                    System.out.println(text);
//                }

                String target = coordinates.toString().replaceAll("\t", "").replaceAll("\n", "").replaceAll(" ", "")
                        .replaceAll("1193\\.", "113.").replaceAll("],\\[", "]@[")
                        .substring(1);
                target = target.substring(0, target.length() - 1);
                List<String> targetList = Arrays.asList(target.split("@"));
                List<String> handledList = CollectionUtil.newArrayList();
                for (String t : targetList) {
                    String[] location = t.replace("[", "").replace("]", "").split(",");
                    try {
                        String trans = JSONUtil.toJsonStr(CoordinatesTransformUtil.transformWGS84ToBD09(Double.valueOf(location[0]), Double.valueOf(location[1])));

                        String handled = trans.replace("[", "{lng:").replace(",", ", lat:").replace("]", "}");
                        handledList.add(handled);
                    } catch (Exception e) {
                        System.err.println(JSONUtil.toJsonStr(location));
                    }
                }

                String lnglat = "[" + handledList.stream().collect(Collectors.joining(",")) + "]";
                System.out.println(text + "     " + lnglat);
                String[] r = new String[2];
                r[0] = text;
                r[1] = lnglat;
                resultList.add(r);
            }

        });

        CsvWriter writer = CsvUtil.getWriter("C:/Users/1/Desktop/testWrite.csv", CharsetUtil.CHARSET_UTF_8);
        //按行写出
        writer.write(resultList);
    }
}
